﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
namespace LMY {
    public class NetTools {
        /// <summary>
        /// 计算字符串MD5值，返回小写格式
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns></returns>
        public static string GetHash(string str) {
            System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
            byte[] bytes = ue.GetBytes(str);

            // encrypt bytes
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashBytes = md5.ComputeHash(bytes);

            // Convert the encrypted bytes back to a string (base 16)
            string hashString = "";

            for (int i = 0; i < hashBytes.Length; i++)
            {
                hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
            }

            return hashString.PadLeft(32, '0');

        }

        public static byte[] GetBytesFromString(string str) {
            System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
            return ue.GetBytes(str);
        }

        public static string GetStringFromBytes(byte[] bytes) {
            return System.Text.Encoding.Default.GetString(bytes);
        }

        /// <summary>
        /// 将文件转换为字节流
        /// </summary>
        /// <param name="url">文件地址（包含文件名及扩展名）</param>
        /// <returns>字节流</returns>
        public static byte[] GetBytes(string url) {
            if (File.Exists(url) == false)
                return null;

            byte[] fileByte = File.ReadAllBytes(url);

            if (fileByte == null)
                return null;
            return fileByte;
        }

        /// <summary>
        /// 将字节流转换为MD
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns>大写格式MD5</returns>
        public static string GetHashFromBytes(byte[] bytes) {
            if (bytes == null)
                return null;

            byte[] hashByte = new MD5CryptoServiceProvider().ComputeHash(bytes);

            return byteArrayToString(hashByte);
        }

        /// <summary>
        /// 计算文件的md5值，返回大写格式
        /// </summary>
        /// <param name="url">文件地址（包含文件名及扩展名）</param>
        /// <returns></returns>
        public static string GetHashFromFile(string url)
        {
            if (File.Exists(url) == false)
                return string.Empty;

            byte[] fileByte = File.ReadAllBytes(url);

            if (fileByte == null)
                return string.Empty;

            byte[] hashByte = new MD5CryptoServiceProvider().ComputeHash(fileByte);

            return byteArrayToString(hashByte);
        }
       
        /// <summary>
        /// 输出数据的十六进制字符串
        /// </summary>
        /// <param name="arrInput"></param>
        /// <returns></returns>
        private static string byteArrayToString(byte[] arrInput)
        {
            StringBuilder sOutput = new StringBuilder(arrInput.Length);

            for (int i = 0; i < arrInput.Length; i++)
            {
                sOutput.Append(arrInput[i].ToString("X2"));
            }
            return sOutput.ToString();
        }

        /// <summary>
        /// 将字节流转换为文件
        /// </summary>
        /// <param name="path">地址包含文件名和扩展名</param>
        /// <param name="fileByte">字节流</param>
        /// <returns></returns>
        public static bool byteToFile(string path, byte[] fileByte) {
            bool isSuccess = false;
            if (fileByte != null) {
                try
                {
                    FileStream file = File.Create(path);
                    file.Write(fileByte, 0, fileByte.Length);
                    isSuccess = true;
                    file.Close();
                }
                catch {

                }
            }
            return isSuccess;
        }
    }
}
